
package com.keshe.security;


import com.keshe.pojo.User;
import com.keshe.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Service
public class SpringSecurityUserService implements UserDetailsService {
    //查找服务，实现查询数据库
    @Autowired
    private UserService userService;
//
//    //根据用户名查询数据库中用户信息
//    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //校验验证码的方法
        System.out.println("用户名："+username);
        User user = userService.findUserByUsername(username);
        if (user == null){
            return null;
        }
        List<GrantedAuthority> authorities = new ArrayList<>();
        org.springframework.security.core.userdetails.User securityUser =
                new org.springframework.security.core.userdetails.User(username, user.getPassword(), authorities);
        System.out.println(securityUser);
        return securityUser;
    }


}

